package com.scm.biz.service._impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scm.biz.bean.Order;
import com.scm.biz.bean.OrderPattern;
import com.scm.biz.bean.OrderTaskPerformer;
import com.scm.biz.bean.ProofingOrder;
import com.scm.biz.bean.ProofingPattern;
import com.scm.biz.bean.SysUser;
import com.scm.biz.dao.CustomerDao;
import com.scm.biz.dao.OrderDao;
import com.scm.biz.dao.OrderPatternDao;
import com.scm.biz.dao.OrderTaskPerformerDao;
import com.scm.biz.dao.ProofingOrderDao;
import com.scm.biz.dao.ProofingPatternDao;
import com.scm.biz.dao.SysUserDao;
import com.scm.biz.dto.MsgNoticeDto;
import com.scm.biz.dto.ProofingPatternDto;
import com.scm.biz.enums.OrderStatusEnum;
import com.scm.biz.enums.OrderTypeEnum;
import com.scm.biz.result.ProofingPatternResult;
import com.scm.biz.service.MsgNoticeService;
import com.scm.biz.service.OrderTaskFlowService;
import com.scm.biz.service.OrderTaskPerformerService;
import com.scm.biz.service.ProofingPatternService;
import com.scm.core.bean.exp.UfdmException;
import com.scm.utils.UfdmDateUtil;
@Service
public class ProofingPatternServiceImpl implements ProofingPatternService {
	@Autowired
	ProofingPatternDao proofingPatternDao;
	@Autowired
	ProofingOrderDao proofingOrderDao;
	@Autowired
	OrderTaskFlowService orderTaskFlowService;
	@Autowired
	OrderPatternDao orderPatternDao;
	@Autowired
	MsgNoticeService msgNoticeService;
	@Autowired
	CustomerDao customerDao;
	@Autowired
	OrderDao orderDao;
	@Autowired
	SysUserDao sysUserDao;
	@Autowired
	OrderTaskPerformerService orderTaskPerformerService;
	public ProofingPattern selectById(Long id) {
		return proofingPatternDao.selectByPrimaryKey(id);
	}
	@Override
	public void update(ProofingPattern proofingPattern) {
		proofingPatternDao.updateByPrimaryKey(proofingPattern);
	}
	@Override
	public List<ProofingPatternResult> selectByProofingOrderId(
			Long proofingOrderId) {
		// TODO Auto-generated method stub
		return proofingPatternDao.selectByProofingOrderId(proofingOrderId);
	}
	@Override
	public void saveMaterial(Long proofingPatternId,Long proofingOrderId,String accessoryDetail) {
		SysUser sysUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("sysUser");
		ProofingPattern proofingPattern = proofingPatternDao.selectByPrimaryKey(proofingPatternId);
		OrderPattern orderPattern = orderPatternDao.selectByPrimaryKey(proofingPattern.getOrderPatternId());
		orderPattern.setAccessoryDetail(accessoryDetail);
		
		orderPatternDao.updateByPrimaryKey(orderPattern);
		ProofingOrder proofingOrder= proofingOrderDao.selectByPrimaryKey(proofingOrderId);
		
		OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
		orderTaskPerformer.setAction("F210");
		orderTaskPerformer.setBusinessId(proofingPattern.getId());
		orderTaskPerformer.setBusinessType(OrderTypeEnum.T2.toString());
		orderTaskPerformer.setCreatedAt(new Date());
		orderTaskPerformer.setPerformer(sysUser.getId());
		orderTaskPerformer.setUpdatedAt(new Date());
		orderTaskPerformer.setOrderId(proofingOrder.getOrderId());
		orderTaskPerformerService.addPerformer(orderTaskPerformer);
		/********************消息***************/
		
		MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
		msgNoticeDto.setTitle("有新的打样订单已安排");
		msgNoticeDto.setContent(customerDao.selectByPrimaryKey(orderDao.selectByPrimaryKey(proofingOrderDao.selectByPrimaryKey(proofingPattern.getProofingOrderId()).getOrderId()).getCustomerId().intValue()).getFullname()
								+ "客户的打样订单已安排工厂生产，请做好批版准备");
		msgNoticeDto.setOrderType(OrderTypeEnum.T2.toString());
		msgNoticeDto.setTaskId(proofingOrder.getId());
		msgNoticeDto.setUserType(1);
		msgNoticeDto.setOrderId(proofingOrder.getOrderId());
		msgNoticeService.insert(msgNoticeDto,"R004");
		
	}
	@Override
	public void saveSizeFile(Long proofingPatternId, String detailDescription,
			Long proofingOrderId) throws UfdmException {
		
		SysUser sysUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("sysUser");
		ProofingPattern proofingPattern = proofingPatternDao.selectByPrimaryKey(proofingPatternId);
		OrderPattern orderPattern = orderPatternDao.selectByPrimaryKey(proofingPattern.getOrderPatternId());
		orderPattern.setDetailDescription(detailDescription);
		
		orderPatternDao.updateByPrimaryKey(orderPattern);
		ProofingOrder proofingOrder= proofingOrderDao.selectByPrimaryKey(proofingOrderId);
		
		OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
		orderTaskPerformer.setAction("F201");
		orderTaskPerformer.setBusinessId(proofingPattern.getId());
		orderTaskPerformer.setBusinessType(OrderTypeEnum.T2.toString());
		orderTaskPerformer.setCreatedAt(new Date());
		orderTaskPerformer.setPerformer(sysUser.getId());
		orderTaskPerformer.setUpdatedAt(new Date());
		orderTaskPerformer.setOrderId(proofingOrder.getOrderId());
		orderTaskPerformerService.addPerformer(orderTaskPerformer);
		/********************消息***************/
		Order order = orderDao.selectByPrimaryKey(proofingOrderDao.selectByPrimaryKey(proofingPattern.getProofingOrderId()).getOrderId());
		MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
		msgNoticeDto.setTitle("尺码表已上传，请安排工厂");
		msgNoticeDto.setContent(sysUser.getName()+"已上传了"+customerDao.selectByPrimaryKey(order.getCustomerId().intValue()).getFullname()
								+ "客户的打样工厂，请尽快上传尺码表，截止时间为"+UfdmDateUtil.dateToString(proofingOrder.getProofingEnddate(), "MM月dd日"));
		msgNoticeDto.setOrderType(OrderTypeEnum.T2.toString());
		msgNoticeDto.setTaskId(proofingOrder.getId());
		msgNoticeDto.setUserType(0);
		msgNoticeDto.setOrderId(proofingOrder.getOrderId());
		msgNoticeService.insert(msgNoticeDto,order.getManager());
	
	}
	@Override
	public void saveMakeStatus(Long proofingPatternId, Long proofingOrderId) {
		ProofingPattern proofingPattern = proofingPatternDao.selectByPrimaryKey(proofingPatternId);
		if(proofingPattern.getStatus() <= OrderStatusEnum.PROOFING_PRICE.value()){
			ProofingOrder proofingOrder= proofingOrderDao.selectByPrimaryKey(proofingOrderId);
			
			proofingPattern.setStatus(OrderStatusEnum.PROOFING_PRICE.value());
			orderTaskFlowService.addPerformer(OrderTypeEnum.T2, "F203",orderDao.selectByPrimaryKey(proofingOrder.getOrderId()).getManager(), proofingPatternId);
			
			/********************消息***************/
			
			MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
			msgNoticeDto.setTitle("打样订单批版通过");
			msgNoticeDto.setContent(sysUserDao.selectByPrimaryKey(proofingOrder.getCreator()).getName()+"销售的"+customerDao.selectByPrimaryKey(orderDao.selectByPrimaryKey(proofingOrderDao.selectByPrimaryKey(proofingPattern.getProofingOrderId()).getOrderId()).getCustomerId().intValue()).getFullname()
									+ "客户的打样订单已通过批版，请报价");
			msgNoticeDto.setOrderType(OrderTypeEnum.T2.toString());
			msgNoticeDto.setTaskId(proofingOrder.getId());
			msgNoticeDto.setUserType(0);
			msgNoticeDto.setOrderId(proofingOrder.getOrderId());
			msgNoticeService.insert(msgNoticeDto,orderDao.selectByPrimaryKey(proofingOrder.getOrderId()).getManager());
			
			SysUser sysUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("sysUser");
			OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
			orderTaskPerformer.setAction("F202");
			orderTaskPerformer.setBusinessId(proofingPattern.getId());
			orderTaskPerformer.setBusinessType(OrderTypeEnum.T2.toString());
			orderTaskPerformer.setCreatedAt(new Date());
			orderTaskPerformer.setPerformer(sysUser.getId());
			orderTaskPerformer.setUpdatedAt(new Date());
			orderTaskPerformer.setOrderId(proofingOrder.getOrderId());
			orderTaskPerformerService.addPerformer(orderTaskPerformer);
		}
		proofingPatternDao.updateByPrimaryKey(proofingPattern);
		
	}
	@Override
	public void saveBidPrice(Long proofingPatternId, String costPrice,String minSalePrice,String salePrice,
			Long proofingOrderId) {
		ProofingPattern proofingPattern = proofingPatternDao.selectByPrimaryKey(proofingPatternId);
		if(costPrice !=null &&!costPrice.isEmpty()){
			proofingPattern.setCostPrice(new BigDecimal(costPrice));
		}
		if(minSalePrice !=null &&!minSalePrice.isEmpty()){
			proofingPattern.setMinSalePrice(new BigDecimal(minSalePrice));
		}
		if(salePrice !=null &&!salePrice.isEmpty()){
			proofingPattern.setSalePrice(new BigDecimal(salePrice));
		}
		if(proofingPattern.getStatus() <= OrderStatusEnum.FINISH.value()){
			proofingPattern.setStatus(OrderStatusEnum.FINISH.value());
			
			/********************消息***************/
			ProofingOrder proofingOrder= proofingOrderDao.selectByPrimaryKey(proofingOrderId);
			MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
			msgNoticeDto.setTitle("打样订单的样衣已报价");
			msgNoticeDto.setContent(sysUserDao.selectByPrimaryKey(proofingOrder.getCreator()).getName()+"销售的"+customerDao.selectByPrimaryKey(orderDao.selectByPrimaryKey(proofingOrderDao.selectByPrimaryKey(proofingPattern.getProofingOrderId()).getOrderId()).getCustomerId().intValue()).getFullname()
									+ "客户的打样订单已报价");
			msgNoticeDto.setOrderType(OrderTypeEnum.T2.toString());
			msgNoticeDto.setTaskId(proofingOrder.getId());
			msgNoticeDto.setUserType(1);
			msgNoticeDto.setOrderId(proofingOrder.getOrderId());
			msgNoticeService.insert(msgNoticeDto,"R007");
			msgNoticeDto.setUserType(0);
			msgNoticeDto.setOrderId(proofingOrder.getOrderId());
			msgNoticeService.insert(msgNoticeDto,proofingOrder.getCreator());
			
			SysUser sysUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("sysUser");
			OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
			orderTaskPerformer.setAction("F202");
			orderTaskPerformer.setBusinessId(proofingPattern.getId());
			orderTaskPerformer.setBusinessType(OrderTypeEnum.T2.toString());
			orderTaskPerformer.setCreatedAt(new Date());
			orderTaskPerformer.setPerformer(sysUser.getId());
			orderTaskPerformer.setUpdatedAt(new Date());
			orderTaskPerformer.setOrderId(proofingOrder.getOrderId());
			orderTaskPerformerService.addPerformer(orderTaskPerformer);
		}
		
		proofingPatternDao.updateByPrimaryKey(proofingPattern);
		
		

		
	}
	
	
	@Override
	public List<ProofingPatternDto> getByIds(String[] ids) {
		if(ids==null||ids.length==0){
			return new ArrayList<ProofingPatternDto>();
		}
		List<Long> id=new ArrayList<Long>();
		for(String str:ids){
			id.add(new Long(str));
		}
		return proofingPatternDao.selectByIds(id);
	}
}
